#pragma once
#include <workflow/WFHttpServer.h>
#include "../config/redisconfig.h"
#include "../bean/user.h"
#include <memory>

#include <iostream>
#include <unistd.h>
using namespace std;

struct Arg
{
public:
    User m_user;
    std::mutex m_locker;
    unsigned short counter = 0;
    std::string m_token;
    std::string m_kExpireTime;
    Arg(const User &user, const std::string &token, const std::string &kExpireTime)
        : m_user(user), m_token(token), m_kExpireTime(kExpireTime)
    {
    }
};

class LoginRedis
{
private:
    // 缓存最大时长
    const std::string kExpireTime = "1800";
    bool isFinished = false;
    std::mutex locker;
    std::condition_variable vb;
    std::string m_token;
    std::string m_pwd;
    bool user_is_in_redis = false;
    static void redis_callback(WFRedisTask *task);
    static void redis_newExpireUser_callback(WFRedisTask *task);

public:
    LoginRedis();
    ~LoginRedis();
    // 检查缓存中是否存在对应的用户昵称
    bool userIsInRedis(const std::shared_ptr<User> &);
    // 刷新该用户在缓存中的过期时间
    void resetExpireTime(const std::shared_ptr<User> &);
    // 添加用户信息到缓存中
    void newExpireUser(const std::shared_ptr<User> &, const std::string &);
    std::string getToken() { return m_token; }
    std::string getPwd() { return m_pwd; }
};